$.ready(function(){
    var add_data = {};
    $(".operation a").bind('click',editHandler);
    function editHandler(event){
        var event = event || window.event;
        if($(".btn_add").length != 0){
            $(".btn_add").parent().css("text-indent",'3.4em');
            $(".btn_add").parent().parent().find("img").hide();
            var str = '<a href="javascript:void(0)" v="'+add_data.role_type+'">编辑</a>';
            $(".btn_add").parent().html(str);
            $(".operation a").unbind();
            $(".operation a").bind('click',editHandler);
        }

        var obj = $(this);
        obj.parent().parent().find("img").show();
        var role_type = obj.attr("v");
        var type ="add";
        var city_id = $("#city_id").val();

        add_data.type ="add";
        add_data.city_id = city_id;
        add_data.role_type = role_type;
        add_data.user_id = '';
        add_data.user_info = '';

        obj.parent().css("text-indent",'0');
        var str1 =  ' <input type="text" placeholder="请输入姓名" value="" class="txt_add"/>';
        var str2 =  '<input type="button" class="btn_add" value="添&nbsp;加" />';
        obj.replaceWith(str1+ str2);
        btnAdd();
        autoCompleteInput();
        event.stopPropagation();

    };
    $("img[title='删除']").hide();
    $("#search").bind('click',function(){
      //  alert("asdfa");
        var objform = document.createElement("form");

        objform.setAttribute("method","get");
        objform.setAttribute("action","");

        var input_city = document.createElement("input");
        input_city.setAttribute("type","hidden");
        input_city.setAttribute("name","city_id");
        input_city.setAttribute("value",$("#city_id").val());

        var role_type = document.createElement("input");
        role_type.setAttribute("type","hidden");
        role_type.setAttribute("name","role_type");
        role_type.setAttribute("value",$("#role_type").val());

        objform.appendChild(input_city);
        objform.appendChild(role_type);
        document.body.appendChild(objform);
        objform.submit();



    });
    function btnAdd(){
        $(".btn_add").unbind();
        $(".btn_add").bind('click',function(){
            var obj = $(this);
            for(var i in add_data){
                if(!add_data[i]){
                    alert("参数缺失");
                    return;
                   // return;
                }
            }
              jQuery.ajax({
                async:false,
                url:url_con,
                data:add_data,
                type:'post',
                success:function (data) {
                    var data = eval('(' + data + ')');
                    if(data.errcode){//存在员工档案
                        var str1 = '<span class="info"><label role_id="'+data.data+'">'+add_data.user_info+'</label>';
                        var str2 = '<a href="javascript:void(0)"><img style="display: inline-block;" src="'+del_icon+'" alt="删除" title="删除"></a></span>';

                        obj.parent().prev().append(str1+ str2);
                        delHandler();
                        $(".txt_add").val('');
                        add_data.user_id = '';
                        add_data.user_info = '';
                        return;
                    }
                    alert(data.msg);
                    return;
                }
            });
        });


    }

    positionResult = false;
    function autoCompleteInput(){
        //江仑
        $(".txt_add").autocomplete({
            source: function(request, response) {
                $.ajax({
                    url:url_user,
                    dataType: "json",
                    data: {
                        name: request.term
                    },
                    success: function(data) {
                        response( $.map( data.data, function( item ) {
                            //                    response( $.map( testdata, function( item ) {
                            return {
                                label:  item.name ,
                                value:  item.name,
                                user_id: item.user_id,
                                user_info:  item.name//与文档相反

                            }
                        }));
                    }
                });
            },
            select: function( event, ui ) {
                var user_id = ui.item.user_id;
                var job_id = ui.item.job_id;
                add_data.user_id = ui.item.user_id;
                add_data.user_info = ui.item.user_info;
                positionResult = true;
                //event.stopPropagation();
            }
        });
        $(".txt_add").bind("keydown",function(event){
            if((event.keyCode == 13 && positionResult == true)||(event.keyCode == 9 && positionResult == true) || (event.keyCode == 32 && positionResult == true)){
                return;
            }
            add_data.user_id = '';
            add_data.user_info = '';
            positionResult = false;
        });
        $(".txt_add").bind("blur",function(){
            if(!positionResult){
                add_data.user_id = '';
                add_data.user_info = '';
                $(this).val("");

            }
        });
    }
    $("#city_id").bind('change',function(){
        add_data.city_id = $("#city_id").val();
    });
    $("tbody tr").bind("mouseover",function(){
       $(this).addClass("selected");
    });
    $("tbody tr").bind('click',function(event){
        var event = event || window.event;
        event.stopPropagation();

    });
    $("body").bind("click",function(event){
        if(event.target == document.body && $(".btn_add").length != 0){
            var obj =  $(".btn_add").parent();
            $(".btn_add").parent().css("text-indent",'3.4em');
            $(".btn_add").parent().parent().find("img").hide();
            var str = '<a href="javascript:void(0)" v="'+add_data.role_type+'">编辑</a>';
            $(".btn_add").parent().html(str);
            obj.find("a").bind('click',editHandler);

        }
        /*var event = event || window.event;
        if(event.target.className === "ui-corner-all ui-state-focus"){
            return;
        }
        if($(".btn_add").length != 0){
            var obj =  $(".btn_add").parent();
            $(".btn_add").parent().css("text-indent",'3.4em');
            $(".btn_add").parent().parent().find("img").hide();
            var str = '<a href="javascript:void(0)" v="'+add_data.role_type+'">编辑</a>';
            $(".btn_add").parent().html(str);
            obj.find("a").bind('click',editHandler);

        }*/
    });
    $("tbody tr").bind("mouseout",function(){
        $(this).removeClass("selected");
    });
    delHandler();
    function delHandler(){
    $("img[title='删除']").bind('click',function(){
        var obj = $(this);
        var type ='del';
        var role_id = $(this).parent().prev().attr("role_id");
        jQuery.ajax({
            async:false,
            url:url_con,
            data:{type:'del',role_id:role_id},
            type:'post',
            success:function (data) {
                var data = eval('(' + data + ')');

                if(data.errcode){
                   obj.parent().parent().remove();
                }


            }
        });


    });}
}());